package com.example.amoxicilin.null_test.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Answer;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.domain.User;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.util.List;

import static com.example.amoxicilin.null_test.main.MeFragment.SHOW_RESPONSE;

public class MyAnswerActivity extends AppCompatActivity {

    private int usernumber;
    private RecyclerView recyclerView;

    List<String> answerlist;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            String response=(String) msg.obj;
            answerlist = JsonTools.jsonSplit(response);
            recyclerView.setAdapter(new RecyclerView.Adapter()
            {
                class MyViewHolder extends RecyclerView.ViewHolder{

                    public final ImageView head;
                    public final TextView content;
                    public final TextView person;
                    public final TextView upvotenumber;
                    public final ImageView delete;
                    public MyViewHolder(View itemView) {
                        super(itemView);
                        head=(ImageView)itemView.findViewById(R.id.my_answer_person_img);
                        content=(TextView)itemView.findViewById(R.id.my_answer_content);
                        person=(TextView)itemView.findViewById(R.id.my_answer_person);
                        upvotenumber=(TextView)itemView.findViewById(R.id.my_answer_upvotenumber);
                        delete=(ImageView)itemView.findViewById(R.id.my_answer_delete);
                    }
                }


                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_my_answer, parent, false);
                    return new MyViewHolder(view);
                }

                @Override
                public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
                    MyViewHolder myViewHolder=(MyViewHolder) holder;
                    final Answer answer = JsonTools.getAnswer("answer", answerlist.get(position));//获取答案对象
                    int usernumber;
                    usernumber=answer.getPerson();
                    String path2 = "http://120.24.94.172/JsonTestServer/jsonTest?type1=query&type2=user&key=number&content="+usernumber;//每个回答对应的人
                    String jsonString2 = HttpUtils.getJsonContent(path2);
                    User user=JsonTools.getUser(null,jsonString2);
                    myViewHolder.person.setText(user.getName());
                    myViewHolder.content.setText(answer.getContent());
                    myViewHolder.content.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                    holder.itemView.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View v) {
                            Intent intent =new Intent();
                            intent.setClass(MyAnswerActivity.this,AnswerActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putString("AnswerContent",answer.getContent());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                    myViewHolder.upvotenumber.setText(""+answer.getAnumber());

                    myViewHolder.delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Snackbar.make(v,"确定要删除吗？",Snackbar.LENGTH_SHORT).setAction("确认", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String deletepath="http://120.24.94.172/JsonTestServer/jsonTest?" +
                                            "type1=delete&type2=answer&anumber="+answer.getAnumber();
                                    Log.i("nimabi", "onClick: "+deletepath);
                                    Log.i("answerlist", "list:"+answerlist);
                                    String deleteResponse=HttpUtils.getJsonContent(deletepath);
                                    Status status=JsonTools.getStatus(null,deleteResponse);
                                    if(status.getStatusId()==1){
                                        Snackbar.make(v, "删除成功！", Snackbar.LENGTH_LONG).show();
                                    }
                                    else
                                        Snackbar.make(v, "删除失败！", Snackbar.LENGTH_LONG).show();
                                }
                            }).show();
                        }
                    });

                }

                @Override
                public int getItemCount() {
                    return answerlist.size();
                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_answer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        Bundle bundle=getIntent().getExtras();
        usernumber=bundle.getInt("usernumber");
        recyclerView=(RecyclerView)findViewById(R.id.my_answer_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));



        initData();
    }

    private void initData(){
        String path = "http://120.24.94.172/JsonTestServer/jsonTest?" +
                "type1=query&type2=answer&key=person_number&content="+usernumber;
        String jsonString = HttpUtils.getJsonContent(path);
        Message message= new Message();
        message.what=SHOW_RESPONSE;
        message.obj=jsonString;
        handler.sendMessage(message);
    }

}
